# -*- coding: utf-8 -*-

# graphic-pppoe-client - Easy graphic networking manager
#
# Author: GoTLiuM InSPiRiT <gotlium@gmail.com>
#
# Copyright 2010, GoTLiuM InSPiRiT <gotlium@gmail.com>.
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 2, as published
# by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranties of
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
# PURPOSE.  See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program.  If not, see <http://www.gnu.org/licenses/>.

from los import system
from re import findall
from libraries.net import getDns

IPTABLES = '/sbin/iptables'

def addPort(ifName, port):
	if not findall('dpt:%d' % port, system('%s -L INPUT -n' % IPTABLES, True)):
		system('%s -A INPUT -i %s -p tcp --dport %d -j ACCEPT' % (IPTABLES, ifName, port))
		system('%s -A INPUT -i %s -p udp --dport %d -j ACCEPT' % (IPTABLES, ifName, port))
		return True
	return False

def delPort(ifName, port):
	if findall('dpt:%d' % port, system('%s -L INPUT -n' % IPTABLES, True)):
		system('%s -D INPUT -i %s -p tcp --dport %d -j ACCEPT' % (IPTABLES, ifName, port))
		system('%s -D INPUT -i %s -p udp --dport %d -j ACCEPT' % (IPTABLES, ifName, port))
		return True
	return False

def cmdRun(rules):
	for cmd in rules.strip().replace('IPT', IPTABLES).split("\n"):
		if cmd.strip():
			print cmd.strip()
			system(cmd.strip())

def flushAllRules():
	rules = """
		IPT -F
		IPT -t nat -F
		IPT -t filter -F
		IPT -t mangle -F
		IPT -X
		IPT -t nat -X
		IPT -t mangle -X
		IPT -t filter -X
	"""
	cmdRun(rules)

def setStandardRules():
	rules = """
		IPT -P INPUT DROP
		IPT -P OUTPUT ACCEPT
		IPT -P FORWARD DROP
		IPT -A INPUT -i lo -j ACCEPT
		IPT -A OUTPUT -o lo -j ACCEPT
		IPT -A INPUT -p icmp --icmp-type timestamp-request -j DROP
		IPT -A INPUT -p tcp --tcp-flags ALL NONE -j DROP
		IPT -A INPUT -p tcp --tcp-flags ALL ALL -j DROP
		IPT -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
		IPT -A INPUT -p tcp --tcp-flags ACK ACK -m state --state ESTABLISHED,RELATED -j ACCEPT
		IPT -A INPUT -p udp -m state --state ESTABLISHED,RELATED -j ACCEPT
		IPT -A INPUT -p icmp -m state --state ESTABLISHED,RELATED -j ACCEPT
	"""
	cmdRun(rules)

def loadForwarding():
	if not findall('dpt:53', system('%s -t nat -L PREROUTING -n' % IPTABLES, True)):
		system('%s -t nat -I PREROUTING -p udp --dport 53 -j DNAT --to-destination %s' % (IPTABLES, getDns()))
		system('echo 1 > /proc/sys/net/ipv4/ip_forward')
		system('%s -A FORWARD -j ACCEPT' % IPTABLES)

def uloadForwarding():
	system('echo 0 > /proc/sys/net/ipv4/ip_forward')

def enableForwarding(ifName):
	setupForwarding()
	system('%s -t nat -A POSTROUTING -o %s -j MASQUERADE' % (IPTABLES, ifName))

def disableForwarding(ifName):
	system('%s -t nat -D POSTROUTING -o %s -j MASQUERADE' % (IPTABLES, ifName))
	uloadForwarding()

def addUser(ifName, addr, hwaddr):
	setupForwarding()
	system('%s -t nat -A POSTROUTING -s %s -o %s -j MASQUERADE' % (IPTABLES, addr, ifName))

def delUser(ifName, addr, hwaddr):
	system('%s -t nat -D POSTROUTING -s %s -o %s -j MASQUERADE' % (IPTABLES, addr, ifName))
